package com.feng.online_xdclass.controller;

import com.feng.online_xdclass.model.entity.User;
import com.feng.online_xdclass.model.entity.VideoOrder;
import com.feng.online_xdclass.model.request.VideoOrderRequest;
import com.feng.online_xdclass.service.VideoOderService;
import com.feng.online_xdclass.utils.JsonDate;
import com.feng.online_xdclass.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/pri/order")
public class VideoOrderController {
    @Autowired
    private VideoOderService videoOderService;
    /**
     * 下单接口
     * @param videoOrderRequest
     * @return
     */

    @RequestMapping("save")
    public JsonDate saveOrder(@RequestBody VideoOrderRequest videoOrderRequest) {
        Integer videoId = videoOrderRequest.getVideoId();
        User user = UserThreadLocal.get();
        Integer id = user.getId();
        int rows = videoOderService.save(videoId, id);
       if(rows==0){
           return JsonDate.buildError("用户已购买过此商品");
       }
       else
           return JsonDate.bulidSuccess("下单成功");

    }

    /**
     * 查询用户的订单列表
     * @return
     */
    @RequestMapping("list")
    public JsonDate listOrder(){
        User user = UserThreadLocal.get();
        Integer id = user.getId();
        List<VideoOrder> list=videoOderService.listOrderByUserId(id);
        return JsonDate.bulidSuccess(list);
    }
}
